package com.sk.bilibili.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sk.bilibili.other.R;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import com.sk.bilibili.bean.LiveType;
import com.sk.bilibili.mapper.LiveTypeMapper;

/**
 * @author 邵康
 * @date 2021/11/26 16:56
 */
@Service
public class LiveTypeService extends ServiceImpl<LiveTypeMapper, LiveType> {

    @Autowired
    private MinioService minioService;
    private static final String COVER_PATH = "liveType/cover/%s.jpg";
    private static final Pattern COVER_PATTERN = Pattern.compile("/(liveType/cover/.*jpg)");

    public R<Void> add(LiveType liveType) {
        liveType.setCover(minioService.add(liveType.getCoverFile(), COVER_PATH));
        save(liveType);
        return R.success();
    }

    public R<Void> delete(Integer id) {
        LiveType temp = getById(id);
        minioService.delete(temp.getCover(), COVER_PATTERN);
        removeById(id);
        return R.success();
    }

    public R<Void> update(LiveType liveType) {
        LiveType temp = getById(liveType.getId());
        String cover = minioService.update(COVER_PATH, COVER_PATTERN, temp.getCover(), liveType.getCoverFile());
        liveType.setCover(cover);
        updateById(liveType);
        return R.success();
    }

    public R<IPage<LiveType>> page(Integer page, Integer size) {
        return R.success(page(new Page<>(page, size), null));
    }

    public R<List<LiveType>> getAll() {
        return R.success(list(null));
    }

}
